<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\utils\ResData;
use app\admin\model\MenuModel;



/**
 * 菜单控制器
 */
class Menu
{
    // 使用检查登录、检查权限中间件
    protected $middleware = ['checklogin', 'checkpower'];


    /**
     * 菜单列表
     */
    public function list()
    {
        $pid = (int)input('get.pid', 0);
        $list = MenuModel::where('pid', $pid)->order('ord asc')->select();
        return view('menu/index', ['menus' => $list, 'pid' => $pid]);
    }

    /**
     * 显示添加菜单页面
     */
    public function add()
    {
        // 在子菜单列表页点击 添加菜单按钮 时，根据传过来的pid值查询到其父菜单信息
        $pid = input('get.pid', 0);
        $menu = MenuModel::where('id', $pid)->find();
        return view('menu/add', ['pid' => $pid, 'menu' => $menu]);
    }


    /**
     * 添加保存菜单
     */
    public function save()
    {
        $pid = (int)input('post.pid', 0);
        $title = trim(input('post.title', ''));
        $controller = trim(input('post.controller', ''));
        $method = trim(input('post.method', ''));
        $ord = input('post.ord', 0);
        $ishidden = input('post.ishidden', 0);
        $status = input('post.status', 1);

        if (empty($title)) {
            return json(ResData::res(400, '菜单名称不能为空'));
        }
        if ($pid > 0 && empty($controller)) {
            return json(ResData::res(400, '控制器不能为空'));
        }
        if ($pid > 0 && empty($method)) {
            return json(ResData::res(400, '方法不能为空'));
        }

        $item = MenuModel::where('title', $title)->findOrEmpty();
        if (!$item->isEmpty()) {
            return json(ResData::res(400, '菜单已存在'));
        }

        $menu = new MenuModel();
        $res = $menu->save(['pid' => $pid, 'title' => $title, 'controller' => $controller, 'method' => $method, 'ord' => $ord, 'ishidden' => $ishidden, 'status' => $status]);
        if (!$res) {
            return json(ResData::res(500, '添加失败'));
        }
        return json(ResData::res(200, '添加成功'));
    }

    /**
     * 编辑菜单
     */
    public function edit()
    {
        if (request()->isPut()) {
            // 接收数据
            $id = (int)input('id', 0);
            $pid = (int)input('pid', 0);
            $data['title'] = trim(input('title', ''));
            $data['controller'] = trim(input('controller', ''));
            $data['method'] = trim(input('method', ''));
            $data['ord'] = (int)input('ord', 0);
            $data['ishidden'] = (int)input('ishidden', 0);
            $data['status'] = (int)input('status', 1);

            if (empty($data['title'])) {
                return json(ResData::res(400, '菜单名称不能为空'));
            }
            if (empty($data['controller'])) {
                return json(ResData::res(400, '控制器不能为空'));
            }
            if (empty($data['method'])) {
                return json(ResData::res(400, '方法不能为空'));
            }
            // 更新数据
            $res = MenuModel::where('id', $id)->update($data);
            if (!$res) {
                return json(ResData::res(500, '操作失败'));
            }
            return json(ResData::res(200, '操作成功'));
        }



        // get请求返回页面
        $pid = (int)input('get.pid', 0);
        $mid = (int)input('get.mid', 0);
        // 父级菜单
        $parent_menu = MenuModel::where('id', $pid)->find();
        // 当前待编辑的菜单
        $currunt_menu = MenuModel::where('id', $mid)->findOrEmpty();
        if ($currunt_menu->isEmpty()) {
            return json(ResData::res(404, '菜单不存在'), 404);
        }
        return view('menu/edit', ['currunt_menu' => $currunt_menu, 'parent_menu' => $parent_menu, 'pid' => $pid]);
    }

    /**
     * 删除菜单
     */
    public function del()
    {
        $id = (int)input('id', 0);
        $res = MenuModel::destroy($id);
        if (!$res) {
            return json(ResData::res(500, '操作失败'));
        }
        return json(ResData::res(200, '操作成功'));
    }
}